/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package loadcentraltabledata;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

/**
 *
 * @author A7med Kotb
 */
//Class That Generate File From Central Table
public class LoadCentralTableData {

    Element settingelement;
    Connection c;

    public LoadCentralTableData(String type) {

        try {
            LogFile.log(" System ", " now Parsing XML File and Try to get Database Parameters and Also Jar and Script Files Path required for Main Task");
            DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dbuilder = dfactory.newDocumentBuilder();
            Document xmldocument = null;
            if (OSDetector.isWindows()) {
                xmldocument = dbuilder.parse("C:/Users/A7med Kotb/Documents/NetBeansProjects/trunk/Book Dimension Project/src/java/Configuration/winconfig.xml");
            }
            if (OSDetector.isUnix()) {
                xmldocument = dbuilder.parse("/home/ahmed/NetBeansProjects/trunk/Book Dimension Project/src/java/Configuration/linconfig.xml");
            }
            settingelement = xmldocument.getDocumentElement();
            Element mysqlUrlElement = (Element) settingelement.getElementsByTagName("mysql-url").item(0);
            Element mysqlUserNameElement = (Element) settingelement.getElementsByTagName("mysql-user-name").item(0);
            Element mysqlPasswordElement = (Element) settingelement.getElementsByTagName("mysql-password").item(0);
            String mysqlUrl = mysqlUrlElement.getTextContent();
            String mysqlUserName = mysqlUserNameElement.getTextContent();
            String mysqlPassword = mysqlPasswordElement.getTextContent();
            DriverManager.registerDriver(new com.mysql.jdbc.Driver());
            c = DriverManager.getConnection(mysqlUrl, mysqlUserName, mysqlPassword);

        } catch (ParserConfigurationException ex) {
            LogFile.log(" System ", " encountered the following Exception while trying to get Database Parameters and Pathes Required from XML File for the Main Task, the Exception is ["+ex+"]");
        } catch (SAXException ex) {
            LogFile.log(" System ", " encountered the following Exception while trying to get Database Parameters and Pathes Required from XML File for the Main Task, the Exception is ["+ex+"]");
        } catch (IOException ex) {
            LogFile.log(" System ", " encountered the following Exception while trying to get Database Parameters and Pathes Required from XML File for the Main Task, the Exception is ["+ex+"]");
        } catch (DOMException ex) {
            LogFile.log(" System ", " encountered the following Exception while trying to get Database Parameters and Pathes Required from XML File for the Main Task, the Exception is ["+ex+"]");
        } catch (SQLException ex) {
            LogFile.log(" System ", " encountered the following Exception while trying to get Database Parameters and Pathes Required from XML File for the Main Task, the Exception is ["+ex+"]");
        }
        if (type.equalsIgnoreCase("book")) {
            loadBookData(c);
        }
        if (type.equalsIgnoreCase("media")) {
            loadMediaData(c);
        }
    }

    //method to load data into file from media table
    public void loadMediaData(Connection c) {
        try {
            LogFile.log(" System ", " found that the Type of the Main Task that will run now is [Media]");
            LogFile.log(" System ", " Now will try to get all required data for Media Main task from The XML File");
            Element loadmediadataelement = (Element) settingelement.getElementsByTagName("load-media-data").item(0);
            Element generatemediadataelement = (Element) settingelement.getElementsByTagName("generated-media-file").item(0);
            String generatedmediafile = generatemediadataelement.getTextContent();
            String mediapath = loadmediadataelement.getTextContent();
            ScriptRunner sr = new ScriptRunner(c, true, false);
            sr.runScript(new FileReader(mediapath), generatedmediafile);
        } catch (DOMException ex) {
            LogFile.log(" System ", " encountered the following Exception while trying to get Data Required from XML File for the Media Main Task, the Exception is ["+ex+"]");
        } catch (FileNotFoundException ex) {
            LogFile.log(" System ", " encountered the following Exception while trying to get Data Required from XML File for the Media Main Task, the Exception is ["+ex+"]");
        }

    }

    //method to load data into file  from book table
    public void loadBookData(Connection c) {
        try {
            LogFile.log(" System ", " found that the Type of the Main Task that will run now is [Book]");
            LogFile.log(" System ", " Now will try to get all required data for Book Main task from The XML File");
            Element loadbookdataelement = (Element) settingelement.getElementsByTagName("load-book-data").item(0);
            Element generatebookdataelement = (Element) settingelement.getElementsByTagName("generated-book-file").item(0);
            String bookpath = loadbookdataelement.getTextContent();
            String generatedbookfile = generatebookdataelement.getTextContent();
            System.out.println("Generated Book File Path : " + generatedbookfile);
            ScriptRunner sr = new ScriptRunner(c, true, false);
            sr.runScript(new FileReader(bookpath), generatedbookfile);
        } catch (DOMException ex) {
            LogFile.log(" System ", " encountered the following Exception while trying to get Data Required from XML File for the Book Main Task, the Exception is ["+ex+"]");
        } catch (FileNotFoundException ex) {
            LogFile.log(" System ", " encountered the following Exception while trying to get Data Required from XML File for the Book Main Task, the Exception is ["+ex+"]");
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        LoadCentralTableData lcd = new LoadCentralTableData(args[0]);
    }
}
